package BookManger;

import java.util.ArrayList;
import java.util.Scanner;

public class BookManger {

    public static void main(String[] args) {
        ArrayList<Book> book = new ArrayList<Book>();
        ArrayList<User> users = new ArrayList<User>();
        ArrayList<Publish> publishes = new ArrayList<Publish>();
        Beginmenu(users,book,publishes);
    }
    public static void Beginmenu(ArrayList<User> users,ArrayList<Book> book,ArrayList<Publish> publishes) {
        boolean flag3 = true;
        while (flag3) {
            System.out.println("欢迎来到图书管理系统");//
            System.out.println("1.登录  2.注册");
            Scanner sc = new Scanner(System.in); //键盘录入选择
            String line = sc.nextLine();
            switch (line) {
                case "1":
                    login(users,book,publishes);
                    flag3 = false;
                    break;
                case "2":
                    enroll(users);//添加管理员
                    break;
            }
        }
    }

    //登录系统，进行操作
    public static void login(ArrayList<User> users,ArrayList<Book> book,ArrayList<Publish> publishes) {
        Scanner sc = new Scanner(System.in);
        String department;
        String username;
        String password;
        boolean flag2 = false;
        while (!flag2) {
            //给出提示登录管理员
            System.out.println("请输入你所在的部门：");
            department = sc.nextLine();
            boolean flag = isUsedapartment(users, department);//判断是否存在该部门
            if (flag) {
                //登录管理员
                System.out.println("请输入用户名：");
                username = sc.nextLine();
                //判断此部门是否有该用户
                boolean flag1 = isUser(users, username);
                if (flag1) {
                    //判断密码是否输入正确
                    System.out.println("请输入密码：");
                    password = sc.nextLine();
                    flag2 = ispassword(users, password);
                    if (flag2) {
                        System.out.println("登录成功");
                        menu(book,users,publishes,username);
                    } else {
                        System.out.println("输入密码错误，请重新输入");
                    }

                } else {
                    System.out.println("该用户不存在，请重新输入");

                }
            } else {
                System.out.println("该部门不存在，请重新输入");
            }
        }
    }

    //管理员注册，定义方法添加管理员
    public static void enroll(ArrayList<User> users) {
        Scanner sc = new Scanner(System.in);
        String department1;
        String usename;
        while (true) {
            //给出提示新增管理员
            System.out.println("请输入所属部门：");
            department1 = sc.nextLine();
            boolean flag = isUsedapartment(users, department1);
            if (flag) {
                //录入管理员用户名
                System.out.println("请输入用户名");
                usename = sc.nextLine();
                boolean flag1 = isUser(users, usename);
                if (flag1) {
                    System.out.println("你输入的用户名在该部门已存在，请重新输入");
                } else {
                    break;
                }
            } else {
                //录入当部门不存在此用户名时的用户名
                System.out.println("请输入用户名");
                usename = sc.nextLine();
                break;
            }
        }
        //录入管理员密码
        System.out.println("请输入密码");
        String password = sc.nextLine();
        //创建管理员对象，将输入的数据赋值给管理员对象的成员变量
        User s = new User();
        s.setUser(usename);
        s.setDepartment(department1);
        s.setPassword(password);
        users.add(s);
        System.out.println("注册成功！");
    }

    public static Boolean isUsedapartment(ArrayList<User> users, String department) {
        boolean flag = false;
        for (int i = 0; i < users.size(); i++) {
            User s = users.get(i);
            if (s.getDepartment().equals(department)) {
                flag = true;
                break;
            }
        }
        return flag;
    }//定义判断部门是否存在的方法

    public static Boolean isUser(ArrayList<User> users, String usename) {
        boolean flag1 = false;
        for (int i = 0; i < users.size(); i++) {
            User s = users.get(i);
            if (s.getUser().equals(usename)) {
                flag1 = true;
                break;
            }
        }
        return flag1;
    }//定义判断用户名是否存在的方法

    public static Boolean ispassword(ArrayList<User> users, String password) {
        boolean flag2 = false;
        for (int i = 0; i < users.size(); i++) {
            User s = users.get(i);
            if (s.getPassword().equals(password)) {
                flag2 = true;
                break;
            }
        }
        return flag2;
    }//定义判断密码是否输入正确


    public static void menu(ArrayList<Book>book,ArrayList<User> users,ArrayList<Publish> publishes,String username) {//选择菜单
        while (true){
            Scanner sc = new Scanner(System.in);
            System.out.println(username + "," + "欢迎您使用龙院书籍管理系统！！！");
            System.out.println("请输入数字进行选择：1 图书管理  2 出版社管理  3 退出登录  4 退出系统");
            int line1 = sc.nextInt();
            switch (line1) {
                case 1:
//                System.out.println("图书管理");
                    BookMangerMenu(book,users,publishes);
                    break;
                case 2:
                    System.out.println("出版社管理");
                    Pmanagement(users, book,publishes);
                    break;
                case 3:
                    System.out.println("退出登录成功！");
                    Beginmenu(users,book,publishes);
                    break;
                case 4:
                    System.out.println("系统退出成功");
                    System.exit(0);
            }
        }

    }

    public static void BookMangerMenu(ArrayList<Book> book,ArrayList<User> users,ArrayList<Publish> publishes) {
        while (true) {
            System.out.println("请输入：1.增加 2.删除 3.更新 4.查询 5.返回上一级菜单");
            Scanner sc = new Scanner(System.in);
            int line = sc.nextInt();
            switch (line) {
                case 1:
                    addBook(book);
                    break;
                case 2:
                    deletBook(book);
                    break;
                case 3:
                    update(book);
                    break;
                case 4:
                    find(book,users,publishes);
                    break;
                case 5:
                    System.out.println("返回上一级菜单");
                    menu(book,users,publishes,"管理员"); // 无法引用原来的username(问题3)
                    break;
            }

        }
    }

    public static void Pmanagement(ArrayList<User> users,ArrayList<Book> book,ArrayList<Publish> publishes) {
        while (true){
            System.out.println("请输入：1.增加 2.删除 3.更新 4.根据出版社名称查询 5.查询所有出版社 6.返回上一级菜单");
            Scanner sc = new Scanner(System.in);
            int line = sc.nextInt();
            switch (line) {
                case 1:
                    Padd(publishes);
                    break;
                case 2:
                    Pdelet(publishes);
//                System.out.println("删除");
                    break;
                case 3:
                    Pupdate(publishes);
//                System.out.println("更新");
                    break;
                case 4:
                    findPname(publishes);
//                System.out.println("根据出版社名称查询");
                    break;
                case 5:
                    Pfindall(publishes);
                    System.out.println("查询所有出版社");
                    break;
                case 6:
                    System.out.println("返回上一级菜单");
                    menu(book,users,publishes,"管理员");
                    break;
            }
        }

    }
    //图书管理
    public static void addBook(ArrayList<Book> book) {
        Scanner sc = new Scanner(System.in);
        String ISBN;
        while (true) {
            //给出提示录入书籍ISBN（ISBN不可重复）
            System.out.println("请输入图书ISBN:");
            ISBN = sc.nextLine();
            boolean flag = isbookISBN(book, ISBN);
            if (flag) {
                System.out.println("该书籍ISBN已存在，请重新输入");
            } else {
                break;
            }
        }
        //录入书籍名称
        System.out.println("请输入书籍名称");
        String bookname = sc.nextLine();
        //录入书籍价格
        System.out.println("请输入书籍价格");
        Double price = sc.nextDouble();
        //录入书籍出版社
        System.out.println("请输入书籍出版社");
        Scanner sc1 = new Scanner(System.in);
        String publisher = sc1.nextLine();//不重新创建对象不执行（问题2）
        //录入书籍作者
        System.out.println("请输入书籍作者");
        String Writer = sc1.nextLine();
        Book s = new Book();
        s.setBookname(bookname);
        s.setPrice(price);
        s.setISBN(ISBN);
        s.setPublisher(publisher);
        s.setWriter(Writer);
        book.add(s);
        System.out.println("添加成功！！！");
    }
    public static boolean isbookISBN(ArrayList<Book> book, String ISBN) {
        boolean flag = false;
        for (int i = 0; i < book.size(); i++) {
            Book s = book.get(i);
            if (ISBN.equals(s.getISBN())) {
                flag = true;
                break;
            }
        }
        return flag;
    }
    public static void deletBook(ArrayList<Book> book) {
        System.out.println("请输入要删除的书本的ISBN");
        Scanner sc = new Scanner(System.in);
        String ISBN = sc.nextLine();
        int index = -1;
        for (int i = 0; i < book.size(); i++) {
            Book s = book.get(i);
            if (s.getISBN().equals(ISBN)) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("无此书籍信息");
        } else {
            book.remove(index);
            System.out.println("删除成功");
        }
    }
    public static void update(ArrayList<Book> book) {
        System.out.println("请输入要修改的书籍的ISBN编号");
        Scanner sc = new Scanner(System.in);
        String ISBN = sc.nextLine();
        int index = -1;
        for (int i = 0; i < book.size(); i++) {
            Book s = book.get(i);
            if (s.getISBN().equals(ISBN)) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("无此书籍编号信息，请重新输入");
        } else {
            //录入新书籍名称
            System.out.println("请输入新书籍名称");
            String bookname = sc.nextLine();
            //录入新书籍价格
            System.out.println("请输入新书籍价格");
            Double price = sc.nextDouble();
            //录入新书籍出版社
            System.out.println("请输入新书籍出版社");
            Scanner sc1 = new Scanner(System.in);
            String publisher = sc1.nextLine();//不重新创建对象不执行（问题2）
            //录入新书籍作者
            System.out.println("请输入新书籍作者");
            String Writer = sc1.nextLine();
            Book s = new Book();
            s.setBookname(bookname);
            s.setPrice(price);
            s.setISBN(ISBN);
            s.setPublisher(publisher);
            s.setWriter(Writer);
            book.set(index, s);
        }
    }
    public static void find(ArrayList<Book> book,ArrayList<User> users,ArrayList<Publish> publishes) {
        while (true) {
            System.out.println("请输入查询种类：1.isbn 2.书名（模糊） 3.出版社 4. 作者 5. 价格范围 6.查询所有 7.返回上一级");
            Scanner sc = new Scanner(System.in);
            int line = sc.nextInt();
            switch (line) {
                case 1:
                    isdnfind(book);
                    break;
                case 2:
                    findname(book);
                    break;
                case 3:
                    findpublish(book);
                    break;
                case 4:
                    findWriter(book);
                    break;
                case 5:
                    pricefind(book);
                    break;
                case 6:
                    findall(book);
                    break;
                case 7:
                    BookMangerMenu(book,users,publishes);
                    break;
            }
        }
    }
    public static void isdnfind(ArrayList<Book> book) {
        if (book.size() == 0) {
            System.out.println("无信息，请录入后再查找");
        }else{
            System.out.println("请输入要查询的ISBN");
            Scanner sc = new Scanner(System.in);
            String ISBN = sc.nextLine();
            for (int i = 0; i < book.size(); i++) {
                Book s = book.get(i);
                if (s.getISBN().equals(ISBN)) {
                    System.out.println("ISBN\t书名\t\t价格\t\t出版社\t\t作者");
                    System.out.println(s.getISBN() + "\t" + s.getBookname() + "\t" + s.getPrice() + "\t" + s.getPublisher() + "\t" + "\t" + s.getWriter());
                }
            }
        }
    }
    public static void findname(ArrayList<Book> book) {
        if (book.size() == 0) {
            System.out.println("无信息，请录入后再查找");
        }else {
            System.out.println("请输入要查找的书名");
            Scanner sc = new Scanner(System.in);
            String bookname = sc.nextLine();
            int n = bookname.length();
            int left = 0;
            int right = n;
            for (int i = 0; i < book.size(); i++) {
                Book s = book.get(i);
                String bookname1 = s.getBookname();
                for (int j = 0; j < bookname1.length() ; j++) {
                    if (bookname1.substring(left,right).equals(bookname)){
                        System.out.println("ISBN\t书名\t\t价格\t\t出版社\t\t作者");
                        System.out.println(s.getISBN() + "\t" + s.getBookname() + "\t" + s.getPrice() + "\t" + s.getPublisher() + "\t" + "\t" + s.getWriter());
                        break;
                    }else {
                        left++;
                        right++;
                    }
                }

            }
        }

    }
    public static void findpublish(ArrayList<Book> book){
        if (book.size() == 0) {
            System.out.println("无信息，请录入后再查找");
        }else {
            System.out.println("请输入查询的书籍的出版社");
            Scanner sc = new Scanner(System.in);
            String publish = sc.nextLine();
            System.out.println("ISBN\t书名\t\t价格\t\t出版社\t\t作者");
            for (int i = 0; i < book.size(); i++) {
                Book s = book.get(i);
                if (s.getPublisher().equals(publish)){
                    System.out.println(s.getISBN() + "\t" + s.getBookname() + "\t" + s.getPrice() + "\t" + s.getPublisher() + "\t" + "\t" + s.getWriter());
                }
            }
        }
    }
    public static void findWriter(ArrayList<Book> book){
        if (book.size() == 0) {
            System.out.println("无信息，请录入后再查找");
        }else{
            System.out.println("请输入查询的作者");
            Scanner sc = new Scanner(System.in);
            String Writer = sc.nextLine();
            System.out.println("ISBN\t书名\t\t价格\t\t出版社\t\t作者");
            for (int i = 0; i < book.size(); i++) {
                Book s = book.get(i);
                if (s.getWriter().equals(Writer)){
                    System.out.println(s.getISBN() + "\t" + s.getBookname() + "\t" + s.getPrice() + "\t" + s.getPublisher() + "\t" + "\t" + s.getWriter());
                }
            }
        }

    }
    public static void pricefind(ArrayList<Book> book){
        if (book.size() == 0) {
            System.out.println("无信息，请录入后再查找");
        }else{
            System.out.println("请输入最低价格");
            Scanner sc = new Scanner(System.in);
            Double lowprice = sc.nextDouble();
            System.out.println("请输入最高价格");
            Double higherprice = sc.nextDouble();
            System.out.println("ISBN\t书名\t\t价格\t\t出版社\t\t作者");
            for (int i = 0; i < book.size(); i++) {
                Book s = book.get(i);
                if (lowprice <= s.getPrice() && s.getPrice() <= higherprice){
                    System.out.println(s.getISBN() + "\t" + s.getBookname() + "\t" + s.getPrice() + "\t" + s.getPublisher() + "\t" + "\t" + s.getWriter());
                }
            }
        }

    }
    public static void findall(ArrayList<Book> book){
        if (book.size() == 0){
            System.out.println("无信息，请录入后再查询");
        }else {
            System.out.println("ISBN\t书名\t\t价格\t\t出版社\t\t作者");
            for (int i = 0; i < book.size(); i++) {
                Book s = book.get(i);
                System.out.println(s.getISBN() + "\t" + s.getBookname() + "\t" + s.getPrice() + "\t" + s.getPublisher() + "\t" + "\t" + s.getWriter());
            }
        }
    }
    //出版社管理
    public static void Padd(ArrayList<Publish> publishe){
        Scanner sc = new Scanner(System.in);
        String Pname;
        while (true) {
            //给出提示录入出版社名称（出版社不重复）
            System.out.println("请输入出版社名称:");
            Pname = sc.nextLine();
            boolean flag = isPublishname(publishe, Pname);
            if (flag) {
                System.out.println("该出版社已存在，请重新输入");
            } else {
                break;
            }
        }
            //录入出版社地址
            System.out.println("请输入出版社地址");
            String Paddress= sc.nextLine();
            //录入出版社联系人
            System.out.println("请输入出版社联系人");
            String  PContact = sc.nextLine();
            Publish s = new Publish();
            s.setPname(Pname);
            s.setPaddress(Paddress);
            s.setPContact(PContact);
            publishe.add(s);
            System.out.println("出版社添加成功");
    }
    public static Boolean isPublishname(ArrayList<Publish> publishes,String Pname){
        boolean flag = false;
        for (int i = 0; i < publishes.size(); i++) {
            Publish s = publishes.get(i);
            if (Pname.equals(s.getPname())) {
                flag = true;
                break;
            }
        }
        return flag;
    }
    public static void Pdelet(ArrayList<Publish> publishes){
        System.out.println("请输入要删除的出版社的名称");
        Scanner sc = new Scanner(System.in);
        String Pname = sc.nextLine();
        int index = -1;
        for (int i = 0; i < publishes.size(); i++) {
            Publish s = publishes.get(i);
            if (s.getPname().equals(Pname)) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("无此出版社信息删除失败");
        } else {
            publishes.remove(index);
            System.out.println("删除成功");
        }
    }
    public static void Pupdate(ArrayList<Publish> publishes){
        System.out.println("请输入要更新的出版社名称");
        Scanner sc = new Scanner(System.in);
        String Pname = sc.nextLine();
        Publish s1 = new Publish();
        int index = -1;
        for (int i = 0; i < publishes.size(); i++) {
            Publish s = publishes.get(i);
            if (s.getPname().equals(Pname)){
                index = i;
                System.out.println(s1.getPname()+"\t"+s1.getPaddress()+"\t"+s1.getPContact());
                break;
            }
        }
        if (index == -1){
            System.out.println("查无此出版社更新失败");
        }else{
            //录入新出版社名称
            System.out.println("请输入要更新的出版社名称");
            String Pname1 = sc.nextLine();
            //录入新出版社地址
            System.out.println("请输入新出版社地址");
            Scanner sc1 = new Scanner(System.in);
            String Paddress = sc1.nextLine();//不重新创建对象不执行（问题2）
            //录入新出版社联系人
            System.out.println("请输入新出版社联系人");
            String PContact = sc.nextLine();
            Publish s = new Publish();
            s.setPname(Pname1);
            s.setPaddress(Paddress);
            s.setPContact(PContact);
            publishes.set(index,s);
        }
    }
    public static void findPname(ArrayList<Publish> publishes){
        System.out.println("请输入要查找的出版社名称");
        Scanner sc = new Scanner(System.in);
        String Pname = sc.nextLine();
        if (publishes.size() == 0){
            System.out.println("无信息，请录入后再查询");
        }else {
            for (int i = 0; i < publishes.size(); i++) {
                Publish s = publishes.get(i);
                if (s.getPname().equals(Pname)){
                    System.out.println("出版社名称\t\t地址\t\t\t联系人");
                    System.out.println(s.getPname()+"\t"+s.getPaddress()+"\t"+s.getPContact());
                }
            }
        }
    }
    public static void Pfindall(ArrayList<Publish> publishes) {
        if (publishes.size() == 0) {
            System.out.println("无信息，请录入后再查询");
        } else {
            System.out.println("出版社名称\t\t地址\t\t\t联系人");
            for (int i = 0; i < publishes.size(); i++) {
                Publish s = publishes.get(i);
                System.out.println(s.getPname()+"\t"+s.getPaddress()+"\t"+s.getPContact());
            }
        }
    }
}
